package zhang.automobileservice;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Response;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.HttpMethod;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import me.iwf.photopicker.PhotoPicker;
import me.iwf.photopicker.PhotoPreview;
import utils.CircleNetworkImage;
import utils.MyUrl;
import utils.RequestQueueUtils;


public class ExitLoginActivity extends FarterActivity implements View.OnClickListener {

    private ImageButton finish;
    private ImageButton ib_nextclear;
    private ImageButton ib_nextchange;
    private ImageButton ib_nextphone;
    private Button bt_unLogin;
    private RelativeLayout rl_image;
    private RelativeLayout rl_userName;
    private String id;
    ArrayList<String> selectedPhotos = new ArrayList<>();
    private CircleNetworkImage iv_icon;
    private RelativeLayout rl_phone;
    private TextView tv_load;
    private String url;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_exit_login);
       /* CarServerLoginDB db = new CarServerLoginDB(this);
        user = db.queryId();
        id = user.getId();*/
        id = getSharedPreferences("TAGtext", Context.MODE_PRIVATE).getString("id", "1");
        initView();
    }
/*
    @Override
    protected void onResume() {
        super.onResume();
        Intent intent = getIntent();
        id = intent.getStringExtra("id");
        String url = MyUrl.Font_Url + MyUrl.Image + user.getHeadphoto();
        RequestQueueUtils.exectueCircleNetWorkImageView(getApplicationContext(), url, iv_icon);
        tv_name.setText(user.getUserName());
        tv_phone.setText(user.getPhone());
    }*/


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.finish:
                finish();
                break;
            case R.id.rl_image:
                PhotoPicker.builder()
                        .setPhotoCount(1)
                        .start(ExitLoginActivity.this);
                break;
            case R.id.rl_userName:
            case R.id.rl_phone:
                Intent intent1 = new Intent(this, ModifyMessageActivity.class);
                startActivity(intent1);
                break;
            case R.id.tv_load:
                doUpload();
                finish();
                break;
        }
    }

    private void initView() {
        finish = (ImageButton) findViewById(R.id.finish);
        ib_nextclear = (ImageButton) findViewById(R.id.ib_nextclear);
        ib_nextchange = (ImageButton) findViewById(R.id.ib_nextchange);
        ib_nextphone = (ImageButton) findViewById(R.id.ib_nextphone);
        bt_unLogin = (Button) findViewById(R.id.bt_unLogin);

        finish.setOnClickListener(this);
        ib_nextclear.setOnClickListener(this);
        ib_nextchange.setOnClickListener(this);
        ib_nextphone.setOnClickListener(this);
        bt_unLogin.setOnClickListener(this);
        rl_image = (RelativeLayout) findViewById(R.id.rl_image);
        rl_image.setOnClickListener(this);
        rl_userName = (RelativeLayout) findViewById(R.id.rl_userName);
        rl_userName.setOnClickListener(this);
        iv_icon = (CircleNetworkImage) findViewById(R.id.iv_icon);
        iv_icon.setOnClickListener(this);
        rl_phone = (RelativeLayout) findViewById(R.id.rl_phone);
        rl_phone.setOnClickListener(this);
        tv_load = (TextView) findViewById(R.id.tv_load);
        tv_load.setOnClickListener(this);
    }

    private void doUpload() {
        RequestParams params = new RequestParams(MyUrl.Font_Url + MyUrl.post_icon);
        params.addBodyParameter("id", id);
        params.addBodyParameter("headPhoto", new File(selectedPhotos.get(0)), "image/*");
        x.http().request(HttpMethod.POST, params, new Callback.CommonCallback<JSONObject>() {
            @Override
            public void onSuccess(JSONObject o) {
                handler.sendEmptyMessage(0x01);
            }

            @Override
            public void onError(Throwable throwable, boolean b) {
                handler.sendEmptyMessage(0x02);
            }

            @Override
            public void onCancelled(CancelledException e) {
                Log.e("====onCancelled", "" + "onCancelled");
            }

            @Override
            public void onFinished() {
                Log.e("====onFinished", "" + "onFinished");
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK &&
                (requestCode == PhotoPicker.REQUEST_CODE || requestCode == PhotoPreview.REQUEST_CODE)) {
            List<String> photos = null;
            if (data != null) {
                photos = data.getStringArrayListExtra(PhotoPicker.KEY_SELECTED_PHOTOS);
            }
            if (photos != null) {
                selectedPhotos.addAll(photos);
            }
        }

    }
    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what==0x01){
                Toast.makeText(getApplicationContext(),"上传成功",Toast.LENGTH_LONG).show();
                finish();
            }else if(msg.what==0x02){
                Toast.makeText(getApplicationContext(),"上传失败",Toast.LENGTH_LONG).show();
            }
        }
    };

    @Override
    protected void onResume() {
        super.onResume();
        url = MyUrl.Font_Url + MyUrl.Image;
        if (id == null) {
            RequestQueueUtils.exectueCircleNetWorkImageView(this, url + "default.jpg", iv_icon);
        } else {
            String url1 = MyUrl.Font_Url + MyUrl.getData + "id=" + id;
            RequestQueueUtils.exectueJsonRequest(this, url1, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject o) {
                    try {
                        JSONObject object;
                        if ("0".equals(o.getString("code"))) {
                            object = o.getJSONObject("data");
                            String headPic = object.getString("headPic");
                            RequestQueueUtils.exectueCircleNetWorkImageView(getApplicationContext(), url + headPic, iv_icon);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            });
        }
    }
}
